home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Mac Parts / Files / Locations / CopyFile.cp next >
Text File  |  2000-06-23  |  3KB  |  112 lines

  1. // CopyFile.cp
  2.  
  3. #ifndef CopyFile_h
  4. #include "CopyFile.h"
  5. #endif
  6. #ifndef FileLocation_h
  7. #include "FileLocation.h"
  8. #endif
  9. #ifndef CatInfo_h
  10. #include "CatInfo.h"
  11. #endif
  12. #ifndef FileReadingPath_h
  13. #include "FileReadingPath.h"
  14. #endif
  15. #ifndef TentativeFile_h
  16. #include "TentativeFile.h"
  17. #endif
  18. #ifndef FileWritingPath_h
  19. #include "FileWritingPath.h"
  20. #endif
  21. #ifndef TemporaryMemory_h
  22. #include "TemporaryMemory.h"
  23. #endif
  24. #ifndef AsyncFileWriter_h
  25. #include "AsyncFileWriter.h"
  26. #endif
  27. #ifndef FileReadingAheadLoop_h
  28. #include "FileReadingAheadLoop.h"
  29. #endif
  30.  
  31. void CopyFile( const FileLocation& source, const FileLocation& destination )
  32.   {
  33.     Assert( source != destination );
  34.     Assert( source.NameIsValid() );
  35.     Assert( destination.NameIsValid() );
  36.  
  37.     CopyFile( CatInfo( source ), destination );
  38.   }
  39.  
  40. void CopyFile( const CatInfo& source, const FileLocation& destination )
  41.   {
  42.     // First, make sure we can open the source file
  43.         FileReadingPath sourceData( source.Location(), FileReadingPath::dataFork );
  44.         FileReadingPath sourceResources( source.Location(), FileReadingPath::resourceFork );
  45.     
  46.     // Create the destination
  47.         TentativeFile tentativeDestination( destination,
  48.                                                         source.File().Type(),
  49.                                                         source.File().Signature(),
  50.                                                         source.Script() );
  51.  
  52.     // Open the destination before anyone else gets much of a chance
  53.         FileWritingPath destinationData( destination,
  54.                                                     FilePermission::ReadWrite(),
  55.                                                     FileWritingPath::dataFork );
  56.         FileWritingPath destinationResources( destination,
  57.                                                           FilePermission::ReadWrite(),
  58.                                                           FileWritingPath::resourceFork );
  59.     
  60.     // Copy the catalog info quickly, so other programs
  61.     // have less chance of seeing the wrong info.
  62.       {
  63.         CatInfo destinationInfo( source );
  64.         destinationInfo.BeUnseenByFinder();
  65.         destinationInfo.Set( destination );
  66.       }
  67.  
  68.     // Allocate space to the copy
  69.         destinationData.Allocate( source.File().Data().LogicalLength() );
  70.         destinationResources.Allocate( source.File().Resources().LogicalLength() );
  71.     
  72.     uint32 maxBufferSize = Max( source.File().Data().LogicalLength(),
  73.                                          source.File().Resources().LogicalLength() );
  74.     
  75.     TemporaryMemory<512> bufferSpace( maxBufferSize );
  76.  
  77.     // Copy the data fork
  78.       {
  79.         AsyncFileWriter writer( destinationData );
  80.         for ( FileReadingAheadLoop reader( bufferSpace, sourceData );
  81.                 reader.Unfinished();
  82.                 reader++ )
  83.           {
  84.             writer.LaunchWrite( *reader );
  85.             // There's dead time here until
  86.             // writer.ReadyToComplete() && reader.ReadyToAdvance()
  87.             writer.CompleteWrite();
  88.           }
  89.       }
  90.  
  91.     // Copy the resource fork
  92.       {
  93.         AsyncFileWriter writer( destinationResources );
  94.         for ( FileReadingAheadLoop reader( bufferSpace, sourceResources );
  95.                 reader.Unfinished();
  96.                 reader++ )
  97.           {
  98.             writer.LaunchWrite( *reader );
  99.             // Again, dead time here.
  100.             writer.CompleteWrite();
  101.           }
  102.       }
  103.  
  104.     destinationResources.Close();
  105.     destinationData.Close();
  106.  
  107.     tentativeDestination.Commit();
  108.  
  109.     sourceResources.Close();
  110.     sourceData.Close();
  111.   }
  112.